package com.xxx.video.main.fragments;

import android.os.Bundle;

import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.main.activitys.VideoDetailsActivity;
import com.xxx.video.main.adapters.HomeHotVerticalVideoListAdapter;
import com.xxx.video.main.adapters.HomeIndexTypeListAdapter;
import com.xxx.video.main.beans.VideoListBean;
import com.xxx.video.main.presenters.HomeIndexPresenter;
import com.xxx.video.main.presenters.IHomeIndexView;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;

public class HomeIndexFragment extends BaseFragment<HomeIndexPresenter> implements IHomeIndexView, RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener {

    @BindView(R.id.rcv_home_index_fragment)
    RefreshRecyclerView rcvHomeIndexFragment;
    /**
     * adapter
     */
    private ConcatAdapter mConcatAdapter;
    /**
     * type
     */
    private int type;
    /**
     * page
     */
    private int page = 1;
    /**
     * adapter
     */
    private HomeHotVerticalVideoListAdapter mHotVerticalVideoListAdapter;

    @Override
    protected HomeIndexPresenter createPresenter() {
        return new HomeIndexPresenter(getActivity(), this);
    }

    public static HomeIndexFragment newInstance(Bundle args) {
        HomeIndexFragment fragment = new HomeIndexFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirst) {
            isFirst = false;
            initList();
            getPresenter().getRecommendAllList(type, page);
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_home_index;
    }

    @Override
    public void initData(Bundle extras) {
    }

    @Override
    public void refreshData(Bundle extras) {

    }

    @Override
    public void showRootLayoutStatus(int status) {

    }

    @Override
    public void initList() {
        rcvHomeIndexFragment.setLayoutManager(new LinearLayoutManager(getActivity()));
        mConcatAdapter = new ConcatAdapter();
        rcvHomeIndexFragment.setAdapter(mConcatAdapter);
        HomeIndexTypeListAdapter homeIndexTypeListAdapter = new HomeIndexTypeListAdapter(Objects.requireNonNull(getActivity()));
        mConcatAdapter.addAdapter(homeIndexTypeListAdapter);
        homeIndexTypeListAdapter.setOnItemClickListener((o, position, type1) -> {
            type = type1;
            page = 1;
            getPresenter().getRecommendAllList(type, page);
        });
        rcvHomeIndexFragment.setOnRecyclerViewRefreshOrLoadMoreListener(this);
    }

    @Override
    public void initData2View(List<VideoListBean> list) {
        rcvHomeIndexFragment.resetState();
        showLayoutStatus(1);
        if (mHotVerticalVideoListAdapter == null) {
            mHotVerticalVideoListAdapter = new HomeHotVerticalVideoListAdapter(Objects.requireNonNull(getActivity()));
            mConcatAdapter.addAdapter(mHotVerticalVideoListAdapter);
            mHotVerticalVideoListAdapter.setOnItemClickListener((BaseRecycleViewAdapter.OnItemClickListener<VideoListBean>) (hotListBean, position, type) -> startVideoDetailsActivity(hotListBean.fContentId));
        }
        if (page == 1) {
            mHotVerticalVideoListAdapter.setData(list);
        } else {
            mHotVerticalVideoListAdapter.addData(list);
        }
        if (ListUtils.isEmpty(list) || list.size() < 10) {
            rcvHomeIndexFragment.setNoMoreData();
        }
    }

    @Override
    public void startVideoDetailsActivity(String id) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", id);
        intent2Activity(VideoDetailsActivity.class, bundle);
    }

    @Override
    public void onRecyclerviewRefresh() {
        page = 1;
        getPresenter().getRecommendAllList(type, page);
    }

    @Override
    public void onRecyclerviewLoadMore() {
        page++;
        getPresenter().getRecommendAllList(type, page);
    }
}
